package moodish.client.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import moodish.client.gui.listener.MoodSentinel;
import moodish.client.gui.utils.FriendTableModel;

public class MainPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int EMOTION_DIMENSION = 75;
	private MoodishClientGui gui;
	private FriendTableModel model = new FriendTableModel();
	private JPanel centralPanel;
	private JButton sendButton = new JButton("Send Mood");
	private JButton disconnect = new JButton("Log Out");
	private JTextField searchField = new JTextField();
	private JButton searchButton = new JButton(new ImageIcon("./resources/search.PNG"));
	private NewsFeedPane feedPane;
	private Loader selected = new Loader(true);
	private MoodSentinel moodSentinel = new MoodSentinel(this);
	private JButton sendFriendShipButton = new JButton("request");
	private JButton sendUnFriendShipButton = new JButton("request");
	private JTextField reqFriendField = new JTextField();
	private JTextField reqUnFriendField = new JTextField();

	public MainPanel(MoodishClientGui gui){
		this.gui = gui;
		
		disconnect.addActionListener(gui.getSentinel());
		sendButton.addActionListener(gui.getSentinel());
		
		feedPane = new NewsFeedPane(gui);
		
		setLayout(new BorderLayout());
		centralPanel = new JPanel();
		centralPanel.setLayout(new BorderLayout());
		
		add(centralPanel, BorderLayout.CENTER);

		centralPanel.add(friendListPanel(), BorderLayout.WEST);
		centralPanel.add(statusAndNamePane(), BorderLayout.PAGE_START);
		centralPanel.add(southPane(), BorderLayout.SOUTH);
		centralPanel.add(searchPanel(), BorderLayout.EAST);
		centralPanel.add(feedPane, BorderLayout.CENTER);
	}

	private JPanel statusAndNamePane() {
		Loader headImage = new Loader(false);
		Loader loader = new Loader("resources//sky.jpg", true, true, gui.getUser().getNickName(), null); // TODO add mouse listener
		headImage.add(loader);
		headImage.setPreferredSize(new Dimension(getWidth(), 130));
		loader.add(connectPanel(), BorderLayout.EAST);
		return headImage;
	}

	private JPanel connectPanel() {
		JPanel panel = new JPanel(); panel.setOpaque(false);
		panel.add(disconnect);
		return panel;
	}

	private JPanel friendListPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		Border border = BorderFactory.createTitledBorder("Friend List");
		FriendTableModel model = new FriendTableModel();
		gui.getUser().setFriendmodel(model);
		JTable friendTable = new JTable(model);
		JScrollPane scroll = new JScrollPane(friendTable, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		panel.add(scroll);
		panel.setBorder(border);
		panel.setPreferredSize(new Dimension(200, getHeight()));
		return panel;
	}

	private JPanel southPane(){
		JPanel panel = new JPanel(new BorderLayout());
		TitledBorder border = BorderFactory.createTitledBorder("");
		panel.setBorder(border);
		JPanel aux = new JPanel();
		aux.setLayout(new BorderLayout());
		aux.add(emotionPanel(), BorderLayout.CENTER);
		aux.add(sendEmotionsPanel(), BorderLayout.EAST);
		panel.add(aux, BorderLayout.EAST);
		panel.add(friendShipRelationsPane(), BorderLayout.WEST);
		return panel;
	}

	private JPanel emotionPanel(){
		JPanel aux = new JPanel();
		JPanel panel = new JPanel(new BorderLayout());

		File files = new File("./resources/emotions");
		String[] fileNames = files.list();
		int numberOfColumns = 3;
		int numberOfLines = (int) fileNames.length / numberOfColumns;
		GridLayout layout = new GridLayout(numberOfLines, numberOfColumns);
		aux.setLayout(layout);
		panel.setPreferredSize(new Dimension((int)(EMOTION_DIMENSION * (numberOfColumns + 0.4)), EMOTION_DIMENSION * 2));

		for (String imageName : fileNames) {
			if(imageName.endsWith(".png")){
				Loader loader = new Loader(files.getPath() + "/" + imageName, false, true, null, moodSentinel);
				loader.setToolTipText(imageName.subSequence(0, imageName.length() - 4) + "");
				loader.setName(imageName);
				loader.setPreferredSize(new Dimension(EMOTION_DIMENSION, EMOTION_DIMENSION));
				aux.add(loader);
			}
		}
		JScrollPane scroll = new JScrollPane(aux, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		panel.add(scroll);
		return panel;
	}
	
	private JPanel friendShipRelationsPane(){
		JPanel panel = new JPanel(new GridLayout(2, 1));
		reqFriendField.setPreferredSize(new Dimension(75, 25));
		reqUnFriendField.setPreferredSize(new Dimension(75, 25));
		
		JPanel friendship = new JPanel();
		friendship.setBorder(BorderFactory.createTitledBorder("Request FriendShip"));
		friendship.add(reqFriendField);
		friendship.add(sendFriendShipButton);
		sendFriendShipButton.addActionListener(gui.getSentinel());
		
		JPanel unFriendship = new JPanel();
		unFriendship.setBorder(BorderFactory.createTitledBorder("Request unfriendShip"));
		unFriendship.add(reqUnFriendField);
		unFriendship.add(sendUnFriendShipButton);
		sendUnFriendShipButton.addActionListener(gui.getSentinel());
		
		panel.add(friendship); panel.add(unFriendship);
		return panel;
	}

	private JPanel sendEmotionsPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		selected.setBorder(BorderFactory.createTitledBorder("Selected Mood"));
		selected.setPreferredSize(new Dimension(110, 100));
		selected.addComponentListener(moodSentinel);
		JPanel aux = new JPanel(new BorderLayout());
		aux.add(selected, BorderLayout.WEST);
		aux.add(sendButton, BorderLayout.LINE_END);
		panel.add(aux, BorderLayout.WEST);
		panel.setOpaque(false);
		return panel;
	}
	
	private JPanel searchPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		searchField.setPreferredSize(new Dimension(100, 25));
		searchButton.addActionListener(gui.getSentinel()); //Sentinel added TODO verify if is correct
		JPanel aux = new JPanel(new FlowLayout(50));
		aux.add(searchField);
		aux.add(searchButton);
		panel.add(aux, BorderLayout.NORTH);
		JTable table = new JTable(model);
		JScrollPane scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		panel.add(scroll, BorderLayout.CENTER);
		panel.setPreferredSize(new Dimension(200, 200));
		TitledBorder border = BorderFactory.createTitledBorder("Search Friends by Mood");
		panel.setBorder(border);
		return panel;
	}
	
	public JButton getSendFriendShipButton() {
		return sendFriendShipButton;
	}

	public JButton getSendUnFriendShipButton() {
		return sendUnFriendShipButton;
	}


	public JButton getDisconnect() {
		return disconnect;
	}

	public JButton getSendButton() {
		return sendButton;
	}

	public String getMoodToSend() {
		return selected.getImageName();
	}

	public JPanel getSelected() {
		return selected;
	}

	public JButton getSearchButton() {
		return searchButton;
	}
	
	public JTextField getSearchField() {
		return searchField;
	}

	public void updateSelected(String image) {
		this.selected.loadImage(image);
		centralPanel.validate();
		centralPanel.repaint();
		if(selected.getImageName() != null)
			selected.setToolTipText(selected.getImageName());
	}

	public JTextField getReqFriendField() {
		return reqFriendField;
	}

	public JTextField getReqUnFriendField() {
		return reqUnFriendField;
	}

	public NewsFeedPane getFeedPane() {
		return feedPane;
	}
	
	public FriendTableModel getModel() {
		return model;
	}
}
